package cn.congine.wizarpos.mall.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.congine.wizarpos.mall.common.RespMessage;
import cn.congine.wizarpos.mall.model.open.AccessTokenInput;
import cn.congine.wizarpos.mall.model.open.AccessTokenOutput;
import cn.congine.wizarpos.mall.model.open.CommonResultOutput;
import cn.congine.wizarpos.mall.model.open.AccessTokenOutput.Result;
import cn.congine.wizarpos.mall.service.impl.WizarposOpenRestClient;
import cn.congine.wizarpos.mall.utils.CommonUtil;
import cn.congine.wizarpos.mall.utils.SysConstants;
import cn.congine.wizarpos.mall.utils.WxSignUtil;

/**
 * 通用action
 * 
 * @author xudongdong
 *         2015年9月9日 上午1:02:12
 */
@Controller
@RequestMapping(value = "/common")
public class CommonAction {

	@Autowired
	private WizarposOpenRestClient wizarposOpenRestClient;
	
	/**
	 * 获取微信 js api签名数据用
	 * 
	 * @param mid
	 * @param url
	 * @param httpRequest
	 * @return
	 */
	@RequestMapping(value = "/jsconfig")
	@ResponseBody
	public RespMessage jsConfig(
			@RequestParam(value = "mid", required = false) String mid,
			@RequestParam(value = "url", required = true) String url,
			HttpServletRequest httpRequest) {
		RespMessage res = new RespMessage();
		Map<String, Object> data = new HashMap<String, Object>();
		if (StringUtils.isEmpty(mid)) {
			mid = (String) httpRequest.getSession().getAttribute("mid");
		}
		if (StringUtils.isEmpty(mid)) {
			res.setCode(1);
			res.setMessage("系统错误，请重试");
			return res;
		}
		
		// 请求微信api的ticket
		AccessTokenInput jsInput = new AccessTokenInput();
		jsInput.setMid(mid);
		CommonResultOutput jsOutput = (CommonResultOutput) wizarposOpenRestClient.post(jsInput, "/weixin/ticket/js", CommonResultOutput.class);		
		if (jsOutput == null || SysConstants.OPEN_ERROR.equals(jsOutput.getCode())) {// 请求开放平台出错
			res.setCode(1);
			res.setMessage("系统错误，请重试");
			return res;
		} else if (SysConstants.NO_MERCHANT_CODE.equals(jsOutput.getCode())) {// 非法的慧商户号
			res.setCode(2);
			res.setMessage("该商户不存在");
			return res;
		} else {
			Map<String, String> jsResult = (Map<String, String>) jsOutput.getResult();
			String jsTicket = jsResult.get("ticket");
			String appid = jsResult.get("appid");
			data.put("appId", appid);
			Map<String, String> ret = WxSignUtil.commonSign(jsTicket, url,
					WxSignUtil.createTimestamp());
			data.putAll(ret);
			res.setObj(data);
		}
		res.setCode(0);
		res.setMessage("处理成功");
		return res;
	}
	
	/**
	 * 获取微信 js api签名数据用,以及地址签名
	 * 
	 * @param mid
	 * @param url
	 * @param httpRequest
	 * @return
	 */
	@RequestMapping(value = "/addressconfig")
	@ResponseBody
	public RespMessage addressConfig(
			@RequestParam(value = "mid", required = false) String mid,
			@RequestParam(value = "url", required = true) String url,
			HttpServletRequest httpRequest) {
		RespMessage res = new RespMessage();
		Map<String, Object> data = new HashMap<String, Object>();
		if (StringUtils.isEmpty(mid)) {
			mid = (String) httpRequest.getSession().getAttribute("mid");
		}
		if (StringUtils.isEmpty(mid)) {
			res.setCode(1);
			res.setMessage("系统错误，请重试");
			return res;
		}
		
		// 请求微信api的ticket
		AccessTokenInput jsInput = new AccessTokenInput();
		jsInput.setMid(mid);
		CommonResultOutput jsOutput = (CommonResultOutput) wizarposOpenRestClient.post(jsInput, "/weixin/ticket/js", CommonResultOutput.class);		
		if (jsOutput == null || SysConstants.OPEN_ERROR.equals(jsOutput.getCode())) {// 请求开放平台出错
			res.setCode(1);
			res.setMessage("系统错误，请重试");
			return res;
		} else if (SysConstants.NO_MERCHANT_CODE.equals(jsOutput.getCode())) {// 非法的慧商户号
			res.setCode(2);
			res.setMessage("该商户不存在");
			return res;
		} else {
			Map<String, String> jsResult = (Map<String, String>) jsOutput.getResult();
			String jsTicket = jsResult.get("ticket");
			String appid = jsResult.get("appid");
			data.put("appId", appid);
			Map<String, String> ret = WxSignUtil.commonSign(jsTicket, url,
					WxSignUtil.createTimestamp());
			data.putAll(ret);
			res.setObj(data);
			
			AccessTokenOutput output = (AccessTokenOutput) wizarposOpenRestClient.post(jsInput, "/weixin/accesstoken", AccessTokenOutput.class);		
			String accessToken = null;
			if (output != null && SysConstants.OPEN_SUCCESS.equals(output.getCode())) {
				Result apiTicket = output.getResult();
				if (apiTicket != null) {
					accessToken = apiTicket.getToken();
					// weixin js 地址api 签名
					String retLoc = WxSignUtil.signLocation(accessToken, appid, url,
							ret.get("timestamp"), ret.get("noncestr"));
					data.put("addrSign", retLoc);
				} else {
					res.setCode(1);
					res.setMessage("系统错误，请重试");
					return res;			
				}
			} else {
				res.setCode(1);
				res.setMessage("系统错误，请重试");
				return res;
			}
			
		}

		res.setCode(0);
		res.setMessage("处理成功");
		return res;
	}
}
